// 是否参照自己
export const isSelfReferenced = cityInfo => {
  const isFlag = cityInfo.saleCityUuid == cityInfo.templateCityUuid // 本销售城市编码等于本参照城市编码，即参照自己
  return isFlag
}

// 是否主供
export const isSupply = (cityInfo, supplyCityCodes) => {
  const isFlag = supplyCityCodes.includes(cityInfo.saleCityUuid) // 当前城市编码存在于供货城市编码集合中，即该城市为供货城市
  return isFlag
}

// 获取初始化后的参照城市信息
export const getInitTemplateCityInfo = (row, supplyCityCodes) => {
  const isCurSupply = supplyCityCodes.includes(row.cityCode) // 本销售城市是否供货
  let uuid = ''
  let name = ''
  if (isCurSupply) {
    uuid = row.cityCode
    name = row.cityName
  }
  return { uuid, name }
}

// 获取初始化价格比例
const getInitPriceRatio = (isUnify, cityItem, supplyCityCodes) => {
  if (isUnify) return 1
  const infoVo = {
    templateCityUuid: getInitTemplateCityInfo(cityItem, supplyCityCodes).uuid,
    saleCityUuid: cityItem.cityCode
  }
  if (isSelfReferenced(infoVo)) return 1
  return null
}

// 获取初始化后的销售城市列表
export const getInitSalesCitiesList = (list, isUnify, supplyCityCodes) => {
  const initList = list.map(item => ({
    saleAreaUuid: item.areaCode, // 业务区域编码
    saleAreaName: item.areaName, // 业务区域名称
    saleCityUuid: item.cityCode, // 销售城市编码
    saleCityName: item.cityName, // 销售城市名称
    saleProvinceUuid: item.provinceCode, // 销售省编码
    saleProvinceName: item.provinceName, // 销售省名称
    templateCityUuid: isUnify ? '' : getInitTemplateCityInfo(item, supplyCityCodes).uuid, // 参照城市编码（默认初始参照自己销售城市）
    templateCityName: isUnify ? '' : getInitTemplateCityInfo(item, supplyCityCodes).name, // 参照城市名称
    buyPriceRatio: getInitPriceRatio(isUnify, item, supplyCityCodes), // 进价比例
    costPriceRatio: getInitPriceRatio(isUnify, item, supplyCityCodes), // 成本价比例
    isSupply: supplyCityCodes.includes(item.cityCode) ? '1' : '2' // 是否供货
  }))
  return initList
}

// 获取参照相关信息
export const getTemplateCityInfo = (row, supplyCityCodes) => {
  const referCanSupply = supplyCityCodes.includes(row.templateCityUuid) // 参照城市是否供货
  const saleCanSupply = supplyCityCodes.includes(row.saleCityUuid) // 销售城市是否供货
  // 参照城市为空且本销售城市可供货时，参照城市参照自己
  if (!row.templateCityUuid && isSupply(row, supplyCityCodes)) {
    return { templateCityUuid: row.saleCityUuid, templateCityName: row.saleCityName, buyPriceRatio: 1, costPriceRatio: 1 }
  }
  // 本销售城市不供货时，清空参照
  if (!saleCanSupply) {
    return { templateCityUuid: '', templateCityName: '', buyPriceRatio: 1, costPriceRatio: 1 }
  }
  // 参照自己且不供货时，参照清空，价格比例默认1
  if (isSelfReferenced(row) && !referCanSupply) {
    return { templateCityUuid: '', templateCityName: '', buyPriceRatio: 1, costPriceRatio: 1 }
  }
  // 参照其他城市且本销售城市可供货，则参照自己
  if (!isSelfReferenced(row) && isSupply(row, supplyCityCodes)) {
    return { templateCityUuid: row.saleCityUuid, templateCityName: row.saleCityName, buyPriceRatio: 1, costPriceRatio: 1 }
  }
  // 参照其他城市且参照城市不供货，且本销售城市也不供货时，参照清空，价格比例默认1
  if (!isSelfReferenced(row) && !referCanSupply && !isSupply(row, supplyCityCodes)) {
    return { templateCityUuid: '', templateCityName: '', buyPriceRatio: 1, costPriceRatio: 1 }
  }
  // 其余情况不做处理
  return row
}
